﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Framework.Physics.Collision
{
    class Polygon
    {
        private Vector2 position;    // Position
        private List<Vector2> vertices; // Vertices (Objektkoordinaten)

        /// <summary>
        /// Positionsvekto des Polygons
        /// </summary>
        public Vector2 Position { get { return position; } set { position = value; } }

        /// <summary>
        /// Vertex Koordinaten
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Vector2 this[int index]
        {
            get { return GetVertex(index); }
            set { SetVertex(index, value); }
        }

        /// <summary>
        /// Anzahl der Vektoren
        /// </summary>
        public int Count { get { return vertices.Count(); } }


        public Polygon(int x, int y)
        {
            position = new Vector2(x, y);
            vertices = new List<Vector2>();
        }

        /// <summary>
        /// Liefert die Objektkoordinaten
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public Vector2 GetVertex(int n)
        {
            return vertices[n];
        }

        /// <summary>
        /// Liefert die absoluten Koordinaten (Weltkoordinaten)
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public Vector2 GetVertexAbs(int n)
        {
            return Vector2.Add(vertices[n], position);
        }

        /// <summary>
        /// Setzt die Objektkoordinaten
        /// </summary>
        /// <param name="n"></param>
        /// <param name="p"></param>
        private void SetVertex(int n, Vector2 p)
        {
            vertices[n] = p;
        }

        /// <summary>
        /// Fügt ein einen Koordinatenpunkt hinzu
        /// </summary>
        /// <param name="p"></param>
        public void AddVertex(Vector2 p)
        {
            vertices.Add(p);
        }

        /// <summary>
        /// Entfernt eine Koordinate
        /// </summary>
        /// <param name="n"></param>
        public void RemoveVertex(int n)
        {
            vertices.RemoveAt(n);
        }

        /// <summary>
        /// Ein Rechteck wird in ein Polygon "umgewandelt" (Thomas Gröschke)
        /// </summary>
        /// <param name="rectangle"></param>
        /// <returns></returns>
        public static Polygon RectangleZuPolygon(Rectangle rectangle)
        {
            Polygon poly = new Polygon(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2);
            poly.AddVertex(new Vector2(rectangle.Width / 2, -rectangle.Height / 2));
            poly.AddVertex(new Vector2(rectangle.Width / 2, rectangle.Height / 2));
            poly.AddVertex(new Vector2(-rectangle.Width / 2, rectangle.Height / 2));
            poly.AddVertex(new Vector2(-rectangle.Width / 2, -rectangle.Height / 2));

            return poly;
        }

        /// <summary>
        /// Ein Rechteck wird in ein Polygon "umgewandelt" (Thomas Gröschke)
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="breite"></param>
        /// <param name="hoehe"></param>
        /// <returns></returns>
        public static Polygon RectangleZuPolygon(int x, int y, int breite, int hoehe)
        {
            Polygon poly = new Polygon(x + breite / 2, y + hoehe / 2);
            poly.AddVertex(new Vector2(breite / 2, -hoehe / 2));
            poly.AddVertex(new Vector2(breite / 2, hoehe / 2));
            poly.AddVertex(new Vector2(-breite / 2, hoehe / 2));
            poly.AddVertex(new Vector2(-breite / 2, -hoehe / 2));

            return poly;
        }
    }
}
